package com.cloud.util.base;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Arrays;

import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

public class FromGpsToBaidu {

	/**
	 * http://api.map.baidu.com/geoconv/v1/?coords=104.13991928100586,30.6748018738967&from=1&to=5&output=json&ak=HdveE6DXEvvXf9Ppl5Xjwvm73C1hecfX
	 * 将GPS装换坐标
	 * @author Administrator
	 *  from  源坐标类型   取值为如下：                                                                                                                                                                                默认为1，即GPS设备获取的坐标
									1：GPS设备获取的角度坐标;
									2：GPS获取的米制坐标、sogou地图所用坐标;
									3：google地图、soso地图、aliyun地图、mapabc地图和amap地图所用坐标
									
		to	       目的坐标类型      有两种可供选择：5、6。                                                                                                                                                  即bd09ll(百度坐标)
									5：bd09ll(百度经纬度坐标),								
									6：bd09mc(百度米制经纬度坐标);
									
		
		http://api.map.baidu.com/geocoder/v2/?location=30.67856874075,104.14886700231&output=json&ak=HdveE6DXEvvXf9Ppl5Xjwvm73C1hecfX
		将经纬度装换为地理位置信息
		
	 */
//	public static void main(String[] args) {
//		System.out.println(getaddressBylnglat("32.984612,114.065676"));
//		
//	}
	
	    
		public static String getFromGpsToBaiduUseLatLon(String lon,String lat) {
			String result = connectURL("http://api.map.baidu.com/geoconv/v1/?coords="+lon+","+lat+"&from=1&to=5&output=json&ak=HdveE6DXEvvXf9Ppl5Xjwvm73C1hecfX", "");
			
			String cut1 = result.substring(result.indexOf("x") + 3);
			String[] cut2 = cut1.split(",");
			String cut3 = cut1.substring(cut1.indexOf("y") + 3);
			String[] cut4 = cut3.split("}");
			System.out.println(cut2[0]+"=77777=="+cut4[0]);
			return cut2[0]+"==="+cut4[0];
			/*String [] road=null;
			String result2 = connectURL("http://api.map.baidu.com/geocoder/v2/?location=" +cut4[0]+ "," + cut2[0]
					+ "&output=json&ak=HdveE6DXEvvXf9Ppl5Xjwvm73C1hecfX", "");
			 road=	result2.split("\"");
			System.out.println(Arrays.toString(road));*/
		}
		public static String getaddressBylnglat(String lnglat){
			String result = connectURL("http://api.map.baidu.com/geocoder/v2/?location=" +lnglat
					+ "&output=json&ak=HdveE6DXEvvXf9Ppl5Xjwvm73C1hecfX", "");
			JSONObject jsStr = JSONObject.parseObject(result);
			System.out.println(result);
			String str = ((JSONObject)jsStr.get("result")).getString("formatted_address")+((JSONObject)jsStr.get("result")).getString("sematic_description");
			
			return str;
		}
		//根据省 市 县  获取 该地区的经纬度
		public String getFromAddressTolonlat(String Address){
			String addressEn=null;
			try {
				addressEn = URLEncoder.encode(Address,"UTF-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String urlEn = "http://api.map.baidu.com/geocoder/v2/?address="+addressEn+"&output=json&ak=HdveE6DXEvvXf9Ppl5Xjwvm73C1hecfX";
			
			String latlon = connectURL(urlEn,"");
			
			String lon = latlon.substring(latlon.indexOf("lng") + 5,latlon.indexOf("lat")-2);
			String lat = latlon.substring(latlon.indexOf("lat") + 5,latlon.indexOf("}"));
			
			
			return lon+","+lat;
		}
		public static String connectURL(String dest_url, String commString) {
			String rec_string = "";
			URL url = null;
			HttpURLConnection urlconn = null;
			OutputStream out = null;
			BufferedReader rd = null;
			try {
				url = new URL(dest_url);
				urlconn = (HttpURLConnection) url.openConnection();
				urlconn.setReadTimeout(1000 * 30);
				urlconn.setRequestMethod("POST");
				urlconn.setDoInput(true);
				urlconn.setDoOutput(true);
				out = urlconn.getOutputStream();
				out.write(commString.getBytes("UTF-8"));
				out.flush();
				out.close();
				rd = new BufferedReader(new InputStreamReader(urlconn.getInputStream()));
				StringBuffer sb = new StringBuffer();
				int ch;
				while ((ch = rd.read()) > -1)
					sb.append((char) ch);
				rec_string = sb.toString();
			} catch (Exception e) {
				//Log.error(e, e);
				return "";
			} finally {
				try {
					if (out != null) {
						out.close();
					}
					if (urlconn != null) {
						urlconn.disconnect();
					}
					if (rd != null) {
						rd.close();
					}
				} catch (Exception e) {
					//Log.error(e, e);
				}
			}
			return rec_string;
		}
	}
